var app = getApp();
var maxPageIndex;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData:true,
    pageIndex: 1 ,
    result: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.accountInfo();
    
  },

  //对账单信息
  accountInfo: function () {
    var that = this;
    var arr = new Array;
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });
    
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toAccountList.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        userId: app.globalData.userId,
        page: that.data.pageIndex,
        size: 10
      },
      success: function (res) {
        wx.hideLoading();
        maxPageIndex = res.data.pages.total / 10;
        if (res.data.pages.result.length > 0) {
          for (var i = 0; i < res.data.pages.result.length; i++) {
            let createTime = res.data.pages.result[i].createTime.substring(0, 16);
            res.data.pages.result[i].createTime = createTime;
          }
          arr = res.data.pages.result;
          that.setData({ ifShownoData: true });
        } else {
          that.setData({ ifShownoData: false });
        }

        if (that.data.pageIndex != 1) {
          var array = that.data.result;
          array = array.concat(arr);
          that.setData({ ifShownoData: true, result: array });
        } else {
          that.setData({ result: arr });
        }
      },
      fail: function (error) {
        console.log(error);
      }

    })
  },

  //余额详情页
  goBalanceDetail: function (e) {
    var accountNumber = e.currentTarget.dataset.accountnumber;
    var createTime = e.currentTarget.dataset.createtime;
    var price = e.currentTarget.dataset.price;
    var businessType = e.currentTarget.dataset.businesstype;
    var amount = e.currentTarget.dataset.amount.toFixed(2);
    wx.navigateTo({
      url: 'myBalance_detail?accountNumber=' + accountNumber + '&createTime=' + createTime + '&price=' + price + '&businessType=' + businessType + '&amount=' + amount,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      pageIndex: 1, // 每次触发下拉事件pageIndex=1
      result: "",
    });
    that.accountInfo();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (maxPageIndex < (that.data.pageIndex)) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1,
    })
    // 上拉获取更多数据
    that.accountInfo();
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})